<template>
	<view>
		<cu-custom :bgcolor="'#fff'" />
		<view>
			<view class="tip p-4 mb-2">
				<view class="text-danger font-md">
					注意：以下信息必修全部填写,此商家满减的值必须大于3，前端显示的满减金额为满减额度-3.例：满10-7(7这个值设置必须大于3,给前端用户实际的满减为满10-4)</view>
			</view>
			<!-- <scroll-view scroll-y="true" :style="'height:'+scorllHeight+ 'px;'" @scrolltolower="loadMore"> -->
			<view class="shop-list" v-if="shopList && shopList.length > 0">
				<view class="list-box pt-3 pb-3 pl-4 pr-4 mb-2" v-for="(item, index) in shopList" :key="index">
					<view class="list">
						<view class="pt-2 pb-2 d-flex flex-column">
							<view class="d-flex flex-row a-center j-sb span-20 mb-1">
								<view class="mb-1 span-10 d-flex flex-row a-center">
									<text class="span-3">满:</text>
									<uni-data-select class="span-16" v-model="shopList[index].man"
										:localdata="allList[index].xqMoneyList"
										@change="changeXqMoney($event,index)"></uni-data-select>
								</view>
								<view class="mb-1 span-10 d-flex flex-row a-center">
									<text class="span-3">减:</text>
									<uni-data-select v-if="allList[index].otherMoneyList" class="span-17"
										v-model="shopList[index].jian" :localdata="allList[index].otherMoneyList"
										@change="changeOtherMoney($event,index)"></uni-data-select>
								</view>

							</view>
							<view class="d-flex flex-row a-center j-sb mb-2">
								<view class="span-20 ">
									<text class="mb-1 d-block">三方承担:</text>
									<uni-data-select v-if="allList[index].filterMoneyList"
										v-model="shopList[index].commitment"
										:localdata="allList[index].filterMoneyList"></uni-data-select>
								</view>

							</view>

							<view class="d-flex flex-row a-center j-sb span-20">
								<view class="span-9">
									<text class="mb-1 d-block">显示满减:</text>
									<input class="uni-input border rounded" v-model="shopList[index].show_jian"
										placeholder="显示满减满" />
								</view>
								<view class="span-9">
									<text class="mb-1 ml-1 d-block">排序:</text>
									<input class="uni-input border rounded" v-model="shopList[index].list_order"
										placeholder="请输入排序" />
								</view>

							</view>
						</view>
					</view>
					<view class="list-btn mt-3 d-flex flex-row a-center j-end">
						<button v-if="index != 0"
							class="mini-btn m-0 mr-2 input-border-color border bg-none main-text-color" type="default"
							size="mini" @click.stop="toDelete(index)">删除</button>
						<button class="mini-btn m-0 mr-2 input-border-color border bg-none main-text-color"
							type="default" size="mini" @click.stop="add">新增</button>
					</view>
				</view>

			</view>
			<view class="button-box p-2">
				<button class="main-bg-color text-white font-md" @click="submit">提交</button>
			</view>
			<!-- <view class="d-flex a-center j-center text-light-muted font-md py-3">{{loadtext}}</view> -->
			<!-- </scroll-view> -->

		</view>
	</view>
</template>

<script>
	import title from "@/components/common/title.vue"
	import divider from "@/components/common/divider.vue"
	import cuCustom from '@/components/common/custom.vue'
	import interceptor from "@/utils/request.js"
	export default {
		components: {
			title,
			divider,
			cuCustom
		},
		data() {
			return {
				value: 0,
				range: [{
						value: 0,
						text: "分类"
					},
					{
						value: 1,
						text: "二两蟹二两蟹"
					},
					{
						value: 2,
						text: "一两蟹"
					},
				],
				scorllHeight: 500,
				shopList: [{
					man: "",
					jian: "",
					commitment: "",
					show_jian: "",
					list_order: 0
				}],
				loadtext: "上拉加载更多",
				allChecked: false,
				formData: {
					idCard: "",
					name: ""
				},
				allList: [{
					xqMoneyList: [],
					otherMoneyList: [],
					filterMoneyList: []
				}],
				current: 0
			}
		},
		onLoad() {
			// uni.getSystemInfo({
			// 	success: (res => {
			// 		this.scorllHeight = res.windowHeight - uni.upx2px(500)
			// 	})
			// })
			this.getXqMoney()
			this.getRedpacketList()
		},
		onShow() {

		},
		methods: {
			// 上拉加载更多
			loadMore() {
				if (this.loadtext !== '上拉加载更多') return
				this.loadtext = "加载中..."
				setTimeout(() => {
					this.shopList = [...this.shopList, ...this.addlist]
					// 回复状态
					this.loadtext = "上拉加载更多"
				}, 2000)

			},
			add() {
				this.current += 1
				this.shopList.push({
					man: "",
					jian: "",
					commitment: "",
					show_jian: "",
					list_order: 0
				})
				this.allList.push({
					xqMoneyList: [],
					otherMoneyList: [],
					filterMoneyList: [],
				})
				this.getXqMoney()
			},
			toDelete(index) {
				this.shopList.splice(index, 1)
				this.allList.splice(index, 1)
				this.$forceUpdate()
			},
			// 获取满金额
			getXqMoney() {
				interceptor.get('/supplier_applet_api.php?act=selAva').then((res) => {
					res.info.map((item) => {
						item.value = item.val
					})
					this.allList[this.current].xqMoneyList =res.info
				})
			},
			async returnXqMoney() {
				let value = null
				await interceptor.get('/supplier_applet_api.php?act=selAva').then((res) => {
					res.info.map((item) => {
						item.value = item.val
					})
					value = res.info
				})
				return value
			},
			changeXqMoney(e, index) {
				this.current = index
				this.otherMoneyApi(e)
			},
			// 第三方查询接口
			otherMoneyApi(id) {

				interceptor.get('/supplier_applet_api.php?act=selMoney', {
					ava: id
				}).then((res) => {
					res.info.map((item) => {
						item.value = item.val
					})
					this.allList[this.current].otherMoneyList = res.info
				})
			},
			async returnOtherMoneyApi(id) {
				let value = null
				await interceptor.get('/supplier_applet_api.php?act=selMoney', {
					ava: id
				}).then((res) => {
					res.info.map((item) => {
						item.value = item.val
					})
					value = res.info
				})
				return value
			},
			changeOtherMoney(e, index) {
				this.current = index
				this.getFilterAPi()
			},
			// 查询使用条件接口
			getFilterAPi(man, jian) {
				interceptor.get('/supplier_applet_api.php?act=selCommitment', {
					ava: man ? man : this.shopList[this.current].man,
					money: jian ? jian : this.shopList[this.current].jian
				}).then((res) => {
					res.info.map((item) => {
						item.value = item.val
					})
					this.allList[this.current].filterMoneyList = res.info
				})
			},
			async returnGetFilterAPi(man, jian) {
				let value = null
				await interceptor.get('/supplier_applet_api.php?act=selCommitment', {
					ava: man ? man : this.shopList[this.current].man,
					money: jian ? jian : this.shopList[this.current].jian
				}).then((res) => {
					res.info.map((item) => {
						item.value = item.val
					})
					value = res.info
				})
				return value
			},
			getRedpacketList() {
				let _that = this
				_that.allList = []
				_that.shopList = []
				let arr = []
				interceptor.get('/supplier_applet_api.php?act=manjian', {
					supplier_id: uni.getStorageSync('supplier_id')
				}).then((res) => {
					if (res.status === 1) {
						for (let i = 0; i < res.info.manjian_list.length; i++) {
							let item = res.info.manjian_list[i]
							_that.current = i
							item.man = item.man_val
							item.jian = item.jian_val
							item.commitment = item.commitment_val
							_that.allList.push({
								xqMoneyList: [],
								otherMoneyList: [],
								filterMoneyList: []
							})
							 _that.returnXqMoney().then((res) => {
								_that.allList[i].xqMoneyList = res
							 })
							 _that.returnOtherMoneyApi(item.man).then((res) => {
								 _that.allList[i].otherMoneyList = res
							 })
							 _that.returnGetFilterAPi(item.man, item.jian).then((res) => {
								 _that.allList[i].filterMoneyList = res
							 })
							_that.shopList.push({
								man: item.man,
								jian: item.jian,
								commitment: item.commitment,
								show_jian: item.show_jian,
								list_order: item.list_order
							})
						}

						_that.$forceUpdate()
					} else {
						uni.showToast({
							title: res.msg,
							icon: "error"
						})
					}
				})
			},
			submit() {
				let _that = this
				let man = [],
					jian = [],
					commitment = [],
					show_jian = [],
					list_order = []
				this.shopList.map((item) => {
					if(item.man){
						man.push(item.man)
					}
					if(item.commitment){
						commitment.push(item.commitment)
					}
					if(item.jian){
						jian.push(item.jian)
					}
					show_jian.push(item.show_jian)
					list_order.push(item.list_order)
				})
				if(man.length <= 0 ){
					uni.showToast({
						title:"请填写满减条件",
						icon:"none"
					})
					return false
				}
				if(jian.length <= 0 ){
					uni.showToast({
						title:"请填写满减条件",
						icon:"none"
					})
					return false
				}
				if(commitment.length <= 0){
					uni.showToast({
						title:"请填写三方承担条件",
						icon:"none"
					})
					return false
				}
				let params = {
					supplier_id: uni.getStorageSync('supplier_id'),
					man: man,
					jian: jian,
					commitment: commitment,
					show_jian: show_jian,
					list_order: list_order
				}
				interceptor.get('/supplier_applet_api.php?act=manjianSet', params).then((res) => {
					if (res.status === 1) {
						uni.showToast({
							title: res.msg,
							icon: "none",
							success() {
								uni.navigateBack()
							}
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.search-box {
		width: 100%;
		height: 160upx;
		background: #fff;

		box-sizing: border-box;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;

		.input {
			width: 90%;
			border-radius: 20px;
			overflow: hidden;
			border: 1px solid #BBBBBB;
			position: relative;

			.icon-sousuo {
				position: absolute;
				left: 35upx;
				top: 10upx;
			}

			.search {
				position: absolute;
				right: 40upx;
				top: 8upx;
			}
		}
	}

	.shop-list {
		background: #EBECED;
		height: auto;
		padding: 32rpx;
		box-sizing: border-box;

		.list-box {
			background: #FFFFFF;
			border-radius: 20rpx;
			height: auto;

			.list {
				height: auto;
				border-bottom: 1px dashed #dcdcdc;
			}

			.list-btn {}
		}

	}

	::v-deep .uni-select--mask {
		display: none;
	}
</style>